package com.fortune.service.impl;

import com.fortune.constants.Constants;
import com.fortune.constants.RoleConstants;
import com.fortune.dao.DAccountInfoDao;
import com.fortune.dao.DPermissionDao;
import com.fortune.dao.DPostionDao;
import com.fortune.dao.DUserInfoDao;
import com.fortune.dto.in.distributorMgt.*;
import com.fortune.dto.in.producePriceManagement.PriceDealerListDto;
import com.fortune.entity.DPermission;
import com.fortune.service.DAccountInfoService;
import com.fortune.vo.distributorMgt.Distributor;
import com.fortune.vo.producePriceManagement.PriceManagementDealer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class DAccountInfoServiceImpl implements DAccountInfoService {
    @Autowired
    private DAccountInfoDao dAccountInfoDao;
    @Autowired
    private DUserInfoDao dUserInfoDao;
    @Autowired
    private DPostionDao dPostionDao;
    @Autowired
    private DPermissionDao dPermissionDao;

    /**
     * 查询经销商列表
     * @return
     */
    @Override
    public List<Distributor> queryAccountList(DAccountListDto dAccountListDto) {
        return dAccountInfoDao.queryAccountList(dAccountListDto);
    }

    @Override
    public int queryAccountListCount(DAccountListDto dAccountListDto) {
        return dAccountInfoDao.queryAccountListCount(dAccountListDto);
    }

    /**
     * 检测经销商是否存在
     * @param dAccountAddDto
     * @return
     */
    @Override
    public int queryAccountIsExist(DAccountAddDto dAccountAddDto) {
        return dAccountInfoDao.queryAccountIsExist(dAccountAddDto);
    }

    /**
     * 检测经销商是否存在
     * @param phone
     * @return
     */
    @Override
    public int queryAccountByNumber(String phone) {
        return dAccountInfoDao.queryAccountByNumber(phone);
    }

    /**
     * 添加经销商新账户
     * @param dAccountAddDto
     * @return
     */
    @Override
    @Transactional
    public int addAccount(DAccountAddDto dAccountAddDto) {
        //经销商ID
        String dealerId = UUID.randomUUID().toString().replace("-","");
        //司机
        AddRole driver = new AddRole();
        driver.setId(RoleConstants.DRIVER_ID);
        driver.setName(RoleConstants.DRIVER_NAME);
        //经销商
        AddRole saleMan = new AddRole();
        saleMan.setId(RoleConstants.SALSEMAN_ID);
        saleMan.setName(RoleConstants.SALSEMAN_NAME);
        //经理
        AddRole manager = new AddRole();
        manager.setId(RoleConstants.MANAGER_ID);
        manager.setName(RoleConstants.MANAGER_NAME);
        //超级管理员
        AddRole superAdmin = new AddRole();
        superAdmin.setId(RoleConstants.SUPER_ADMIN_ID);
        superAdmin.setName(RoleConstants.SUPER_ADMIN_NAME);
        //内勤
        AddRole inHouse = new AddRole();
        inHouse.setId(RoleConstants.IN_HOUSE_ID);
        inHouse.setName(RoleConstants.IN_HOUSE_NAME);
        //仓库管理员
        AddRole storeOfficer = new AddRole();
        storeOfficer.setId(RoleConstants.STORE_OFFICER_ID);
        storeOfficer.setName(RoleConstants.STORE_OFFICER_NAME);
        List<AddRole> list = new ArrayList<>();
        list.add(driver);
        list.add(saleMan);
        list.add(manager);
        list.add(superAdmin);
        list.add(inHouse);
        list.add(storeOfficer);
        int num = dPostionDao.addPostion(dealerId,list);
        if (num < 1){
            throw new  RuntimeException("新增经销商岗位失败");
        }
        String password = Constants.D_USER_PASSWORD;
        String adminId = RoleConstants.SUPER_ADMIN_ID;
        num = dUserInfoDao.addUserInfo(adminId,dealerId,dAccountAddDto,password);
        if (num < 1){
            throw new  RuntimeException("经销商员工账户失败");
        }
        num = dAccountInfoDao.addAccount(dealerId,dAccountAddDto);
        if (num < 1){
            throw new  RuntimeException("新增经销商账户失败");
        }
        List<DPermission> dPermissionlist = dPermissionDao.queryDPermission();
        if (dPermissionlist!=null && dPermissionlist.size()>0){
            num = dPermissionDao.addDPositionPermission(dealerId,adminId,dPermissionlist);
            if (num < 1){
                throw new  RuntimeException("新增经销商账户失败");
            }
        }
        return 1;
    }

    /**
     * 通过经销商id查找经销商是否存在
     * @param id
     * @return
     */
    @Override
    public int queryAccountById(String id) {
        return dAccountInfoDao.queryAccountById(id);
    }

    /**
     * 删除经销商
     * @param id
     * @return
     */
    @Override
    public int deleteAccount(String id) {
        return dAccountInfoDao.deleteAccount(id);
    }

    /**
     * 编辑经销商
     * @param dAccountEditDto
     * @return
     */
    @Override
    @Transactional
    public int editAccountByDealerIdAndCompanyId(DAccountEditDto dAccountEditDto) {
        String phone = dAccountInfoDao.queryAccountPhoneById(dAccountEditDto.getId());
        int num = dAccountInfoDao.editAccount(dAccountEditDto);
        if (num < 1){
            throw new RuntimeException("编辑失败");
        }
        String password = Constants.D_USER_PASSWORD;
        num = dUserInfoDao.editUser(password,dAccountEditDto);
        if (num < 1){
            throw new RuntimeException("编辑失败");
        }
        return 1;
    }

    /**
     * 重置经销商密码
     * @param acccountId
     * @return
     */
    @Override
    public int resetPassword(String acccountId, String password) {
        return dUserInfoDao.resetPassword(acccountId,password);
    }

    /**
     * 禁用或启用经销商
     * @param userUpAndDownDto
     * @return
     */
    @Override
    public int upAndDownUser(UserUpAndDownDto userUpAndDownDto) {
        return dAccountInfoDao.upAndDownUser(userUpAndDownDto);
    }

    /**
     * 查找经销商详细信息
     * @param id
     * @return
     */
    @Override
    public Distributor queryAccountInfoById(String id) {
        return dAccountInfoDao.queryAccountInfoById(id);
    }

    /**
     * 查询经销商列表
     * @param companyId
     * @return
     */
    @Override
    public List<PriceManagementDealer> queryAccounPricetList(String companyId,PriceDealerListDto priceDealerListDto) {
        return dAccountInfoDao.queryAccounPricetList(companyId,priceDealerListDto);
    }

    /**
     * 经销商名称、简称、编号在该厂商范围内查重
     * @param dAccountEditDto
     * @return
     */
    @Override
    public int queryEditAccountIsExist(DAccountEditDto dAccountEditDto) {
        return dAccountInfoDao.queryEditAccountIsExist(dAccountEditDto);
    }

    /**
     * 手机号码需要全系统查重
     * @param dAccountEditDto
     * @return
     */
    @Override
    public int queryEditAccountByPhone(DAccountEditDto dAccountEditDto) {
        return dAccountInfoDao.queryEditAccountByPhone(dAccountEditDto);
    }

    @Override
    public int queryAccountIsExistByNumber(String number) {
        return dAccountInfoDao.queryAccountIsExistByNumber(number);
    }

    @Override
    public int queryAccountIsExistByAbbreviation(String abbreviation) {
        return dAccountInfoDao.queryAccountIsExistByAbbreviation(abbreviation);
    }

    @Override
    public int queryAccountIsExistByName(String name) {
        return dAccountInfoDao.queryAccountIsExistByName(name);
    }

    @Override
    public int queryEditAccountIsExistNumber(String id, String number) {
        return dAccountInfoDao.queryEditAccountIsExistNumber(id,number);
    }

    @Override
    public int queryEditAccountIsExistByAbbreviation(String id, String abbreviation) {
        return dAccountInfoDao.queryEditAccountIsExistByAbbreviation(id,abbreviation);
    }

    @Override
    public int queryEditAccountIsExistByName(String id, String name) {
        return dAccountInfoDao.queryEditAccountIsExistByName(id,name);
    }

}
